#!/bin/bash

IFS_BAK=$IFS
IFS="
"

inv="/home/xbmc/Gdrive/inventory/up2date"
show="/home/xbmc/TvShows/"



/usr/bin/find $show -iregex '.*\(nfo\)'|/bin/sed 's/.*/"&"/' > $inv/invshow-nfofiles.txt
/usr/bin/find $show -iregex '.*\(nfo\)' > $inv/invshow-nfofiles.txt
/bin/cat $inv/invshow-nfofiles.txt|/bin/grep ' - '|/usr/bin/cut -d "/" -f 5|/usr/bin/sort -u|/bin/sed 's/.*/"&"/' > $inv/invshow.txt



echo "Generating TvShow Inventory from Meta Data - `date`"
echo "Show Title;Studio;Show Rating;Premiered Date;Episode Title;Aired Date;Episode Rating;Season ID;Episode ID;Duration;Height;Width;Audio Channels;Audio Codec;Video Codec;File Extention;File Size(kb)" > $inv/invshow.csv

for fns in `/bin/cat $inv/invshow-nfofiles.txt|/bin/grep ' - '`; do
	a="`echo $fns|/usr/bin/cut -d '/' -f 5`"
	b="`/usr/bin/xml_grep studio --text_only $show/$a/tvshow.nfo`;`/usr/bin/xml_grep rating --text_only $show/$a/tvshow.nfo`;`/usr/bin/xml_grep premiered --text_only $show/$a/tvshow.nfo`"
	aa="`echo $fns |/usr/bin/cut -d '/' -f 6-| /bin/sed 's/ /\\ /g'`"
	aaa="`echo $fns |/usr/bin/cut -d '/' -f 6-|/bin/sed 's/.\{3\}$/*/'| /bin/sed 's/ /\\ /g'`"
	c="`/usr/bin/xml_grep title --text_only $show/$a/$aa`;`/usr/bin/xml_grep aired --text_only $show/$a/$aa`;`/usr/bin/xml_grep rating --text_only $show/$a/$aa`;`/usr/bin/xml_grep season --text_only $show/$a/$aa`;`/usr/bin/xml_grep episode --text_only $show/$a/$aa`;`/usr/bin/xml_grep durationinseconds --text_only $show/$a/$aa`;`/usr/bin/xml_grep height --text_only $show/$a/$aa`;`/usr/bin/xml_grep width --text_only $show/$a/$aa`;`/usr/bin/xml_grep channels --text_only $show/$a/$aa`;`/usr/bin/xml_grep codec --text_only $show/$a/$aa|/bin/sed 'N;s/\n/;/'`"
	e="`/usr/bin/du -l $show/$a/$aaa| /usr/bin/sort -n -r|/usr/bin/head -1 |/usr/bin/cut -d '/' -f 1`"
	d="`/usr/bin/du -l $show/$a/$aaa| /usr/bin/sort -n -r| /usr/bin/head -1 |/usr/bin/cut -d '/' -f 2-|/bin/sed 's/.*\(...\)$/\1/'`"
	echo "$a - `/usr/bin/xml_grep title --text_only $show/$a/$aa` - `date`"
	echo "$a;$b;$c;$d;$e" >> $inv/invshow.csv
done
echo "DONE Generating - `date`"
IFS=$IFS_BAK
IFS_BAK=
